package caidan.data;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import jinyilw.common.Alignment;
import jinyilw.swing.FontTools;
import jinyilw.common.Operator;
import jinyilw.swing.SwingTools;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.layout.OneRowLayout;

public class FlavourPane extends JPanel
{
	private static final long serialVersionUID = -4111799420580989309L;
	private static Dimension spinnerSize = new Dimension(52, 22);
	private JComboBox comboBox = new JComboBox(Flavour.flavourStrings);
	private SpinnerNumberModel qualityNumberModel = new SpinnerNumberModel(0, 0,
			500, 1);
	private JLabel qualityLabel = new JLabel(" 用量(克):");
	private JSpinner qualitySpinner = new JSpinner(qualityNumberModel);
	private ActionListener comboBoxListener;

	private Flavour flavour = Flavour.unChooseFlavour;
	private Operator<Flavour> operator;

	public FlavourPane(Operator<Flavour> operator)
	{
		super(new OneRowLayout(Alignment.LEFT, 1));
		setBorder(RBorderFactory.getDefaultBorder());
		Font font = FontTools.getSystemFont(12);
		qualityLabel.setFont(font);
		comboBox.setFont(font);
		qualityLabel.setFont(font);
		qualitySpinner.setFont(font);
		comboBox.setFocusable(false);
		qualitySpinner.setPreferredSize(spinnerSize);
		SwingTools.makeSpinnerTextCenter(qualitySpinner);
		this.operator = operator;
		JLabel dLabel = new JLabel("调料:");
		dLabel.setFont(font);
		super.add(dLabel);
		super.add(comboBox);
		initListeners();
	}

	private void initListeners()
	{
		comboBoxListener = (ActionEvent e) ->
		{
			setFlavour(Flavour.getFlavour((short) comboBox.getSelectedIndex()));
		};
		comboBox.addActionListener(comboBoxListener);
	}

	public void setFlavour(Flavour flavour)
	{
		if (this.flavour == flavour)
			return;
		this.flavour = flavour;
		comboBox.removeActionListener(comboBoxListener);
		if (flavour == null || flavour == Flavour.unChooseFlavour
				|| flavour.getData() == 0)
		{
			remove(qualityLabel);
			remove(qualitySpinner);
			comboBox.setSelectedIndex(0);
			qualityNumberModel.setValue(0);
			flavour = Flavour.unChooseFlavour;
		} else
		{
			add(qualityLabel);
			add(qualitySpinner);
			comboBox.setSelectedIndex(flavour.getData());
		}
		revalidate();
		repaint();
		comboBox.addActionListener(comboBoxListener);
		if (operator != null)
			operator.operate(this.flavour);
	}

	public void setQuality(short quality)
	{
		qualityNumberModel.setValue(quality);
	}

	public short getQuality()
	{
		return qualityNumberModel.getNumber().shortValue();
	}

	public JSpinner getQualitySpinner()
	{
		return qualitySpinner;
	}

	@Override
	public Component add(Component comp)
	{
		if (comp.getParent() == null)
			super.add(comp);
		return comp;
	}

	public static class FlavourContainer extends Container
	{
		private static final long serialVersionUID = 1465912918860402502L;

		public FlavourContainer(FlavourPane flavourPane1,
				FlavourPane flavourPane2)
		{
			setLayout(new GridLayout(0, 2, 2, 2));
			add(flavourPane1);
			add(flavourPane2);
		}
	}
}